﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GEAR_Structure;

namespace Gear_Base
{
    public partial class AddNewLayerInStratigraphy : Form
    {
        private Project project;
        private Materiale tmp;
        private Materiale layer; 

        public AddNewLayerInStratigraphy(Project _project)
        {
            InitializeComponent();
            this.project = _project;
            this.layer = null;
            bindCombo();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SelectMaterial sm = new SelectMaterial(this.project);
            Materiale m = sm.getSelected(this);
            if (m != null)
            {
                tmp = m;
                bindLayer(tmp);
            }
        }

        /// <summary>
        /// Binding del Layer
        /// </summary>
        /// <param name="l"></param>
        private void bindLayer(Materiale l)
        {
            cmbTipology.SelectedValue = l.Typology;

            chkequivalent.DataBindings.Clear();
            chkequivalent.DataBindings.Add(new Binding("Checked", l, "ConductivityEquivalent"));

            txtDescr.DataBindings.Clear();
            txtDescr.DataBindings.Add(new Binding("Text", l, "Name"));

            txtConductivity.DataBindings.Clear();
            txtConductivity.DataBindings.Add(new Binding("Text", l, "Conductivity"));

            txtConduttance.DataBindings.Clear();
            txtConduttance.DataBindings.Add(new Binding("Text", l, "Conduttance"));

            txtTickness.DataBindings.Clear();
            txtTickness.DataBindings.Add(new Binding("Text", l, "Tickness"));

            txtDensity.DataBindings.Clear();
            txtDensity.DataBindings.Add(new Binding("Text", l, "Density"));

            txtHeat.DataBindings.Clear();
            txtHeat.DataBindings.Add(new Binding("Text", l, "SpecificHeat"));

            txtVaporMax.DataBindings.Clear();
            txtVaporMax.DataBindings.Add(new Binding("Text", l, "VaporPermeabilityMin", true, DataSourceUpdateMode.OnValidation, "E3"));

            txtVaporMin.DataBindings.Clear();
            txtVaporMin.DataBindings.Add(new Binding("Text", l, "VaporPermeabilityMax", true, DataSourceUpdateMode.OnValidation, "E3"));

            txtFatResIgro.DataBindings.Clear();
            txtFatResIgro.DataBindings.Add(new Binding("Text", l, "HygroscopicResistnaceFactor"));

            txtDinamicStiffness.DataBindings.Clear();
            txtDinamicStiffness.DataBindings.Add(new Binding("Text", l, "DynamicStiffness"));
        }

        public Materiale getValue(Form parent)
        {
            base.ShowDialog(parent);
            return layer;
        }

        public Materiale getValue(Form parent,Materiale edit)
        {
            this.tmp = new Materiale(edit);
            bindLayer(tmp);
            base.ShowDialog(parent);
            return layer;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            tmp.Typology = (Materiale.typology_material)cmbTipology.SelectedValue;
            this.layer = tmp;
            this.Close();
        }

        //Binding del Combo per la selezione della tipologia del materiale
        private void bindCombo()
        {
            cmbTipology.DataBindings.Clear();
            cmbTipology.DataSource = EnumHelper.ToList(typeof(Materiale.typology_material));
            cmbTipology.DisplayMember = "Value";
            cmbTipology.ValueMember = "Key";
            cmbTipology.SelectedIndex = -1;
        }
    }
}
